
import 'dart:convert';

import 'package:loxen/mode/user.dart';

T asT<T>(dynamic value) {
  if (value is T) {
    return value;
  }
  return null;
}

class StudyInfo {
  StudyInfo({
    this.type,
    this.title,
    this.id,
    this.category,
    this.content,
    this.add_time,
    this.view_num,
    this.comment_num,
    this.banner,
    this.user,
    this.video,
    this.collect_num,
    this.is_collect,
    this.cover,
    this.brief,
    this.like_num,
    this.is_like});

  factory StudyInfo.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    var id = jsonRes['id'];
    if(id is String){
      id = int.parse(id);
    }else{
      id = id;
    }
    return StudyInfo(
        type: asT<String>(jsonRes['type']),
        id: id,
        title: asT<String>(jsonRes['title']),
        category: asT<int>(jsonRes['category']),
        content: asT<String>(jsonRes['content']),
        add_time: asT<String>(jsonRes['add_time']),
        view_num: asT<int>(jsonRes['view_num']),
        comment_num: asT<int>(jsonRes['comment_num']),
        banner: asT<String>(jsonRes['banner']),
        user: User.fromJson(asT<Map<String, dynamic>>(jsonRes['user'])),
        video: Video.fromJson(asT<Map<String, dynamic>>(jsonRes['video'])),
        collect_num: asT<int>(jsonRes['collect_num']),
        is_collect: asT<int>(jsonRes['is_collect']),
        cover: asT<String>(jsonRes['cover']),
        brief: asT<String>(jsonRes['brief']),
        like_num: asT<int>(jsonRes['like_num']),
        is_like: asT<int>(jsonRes['is_like']),
    );
  }

  List<dynamic> contentList;
  String type;
  String title;
  int id;
  int category;
  String content;
  String add_time;
  int view_num;
  int comment_num;
  String banner;
  User user;
  Video video;
  int collect_num;
  int is_collect;
  String cover;
  String brief;
  int like_num;
  int is_like;

  Map<String, dynamic> toJson() =>
      <String, dynamic>{
        'type': type,
        'id': id,
        'title': title,
        'category': category,
        'content': content,
        'view_num': view_num,
        'add_time': add_time,
        'comment_num': comment_num,
        'banner': banner,
        'user': user,
        'video': video,
        'collect_num': collect_num,
        'is_collect': is_collect,
        'cover': cover,
        'brief': brief,
        'like_num': like_num,
        'is_like': is_like,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class Video {
  Video({
    this.id,
    this.video_url,
  });

  factory Video.fromJson(Map<String, dynamic> jsonRes) => jsonRes == null
      ? null
      : Video(
      id: asT<int>(jsonRes['id']),
      video_url: asT<String>(jsonRes['video_url']),
      );

  int id;
  String video_url;


  Map<String, dynamic> toJson() => <String, dynamic>{
    'id': id,
    'video_url': video_url,
  };

  @override
  String toString() {
    return json.encode(this);
  }
}



